<?php
namespace dg\Controller;
use Think\Controller;

class UserController extends ApibaseController {
	/*
	 * 登陆模块
	 */
	public function login(){
		global $isLogin;
		if($isLogin){
            $role_id = session('role_id');
            redirect('/index/input?res='.($role_id==6?'0-1':'0-2'));
        }else{
    		$this->display('user/login');
        }
    }

  
    /*
     * 系统登陆
     */
    public function checklogin(){
    	$userMsg = M('ResEntity')->where(array('username'=>I('post.username')))->where('is_cash = 0')->find();
        if(!$userMsg['is_active']){
            jsonRespons('0','您的账号已被冻结！');
        }
    	if($userMsg['password'] == md5(I('post.passwd').$userMsg['salt'])){
    		$pushSession = array('name'=>I('post.username'));
    		if(I('post.signlogin') == 1){
    			$setSecret = md5(I('post.username').'ajwUid'.time());
    			M('ResEntity')->where(array('username'=>I('post.username')))->setField(
    				array('remember_token' => $setSecret)
    			);
    			cookie('_ajwunion',$setSecret,'expire=604800');
    			$pushSession['expire'] = 604800;
    		}
            $role_id = M('AuthGroupAccess')->where(array('uid'=>$userMsg['rid']))->field('role_id')->find();
    		session($pushSession);
            session('role_id',$role_id['role_id']);
    		session('loginUid',$userMsg['rid']);
    		session('loginName',I('post.username'));
    		cookie('ajwUid',base64_encode(I('post.username')).randstr(),'expire=604800');
    		session(I('post.username'),true);
    		putlog('登陆系统','UserController','checklogin');
    		jsonRespons('1','登陆成功！');
    	}else{
    		jsonRespons('0','帐号或密码有误！');
    	}
    }
    



	
    /*
     * 系统注消操作
     */
    public function loginout(){
    	$userMsg = getLoginMsg();
    	session(cookie('PHPSESSID'),null);
        session('role_id',null);
    	session(getLoginMsg()->username,null);
        session('loginUid',null);
        session('loginName',null);
    	cookie('ajwUid',null);
    	cookie('_ajwunion',null);
    	cookie('PHPSESSID',null);

    	redirect('/user/login');
    }
    
    /*
     * 个人中心简介
     * @date: 2015-3-23下午2:30:20
     * @editor: XING
     */
    public function usercenter(){
    	
    	//个人页面渲染
    	$this->display('user/usercenter');
    }
    
    /*
     * 个人中心资料编辑
     * @date: 2015-3-23下午5:06:13
     * @editor: XING
     */
    public function userUpdate(){
    	if(I('post.update')){
    		$data = array('email'=>I('post.email'));
    		if(I('post.password')){
    			$salt = randstr(5);
    			$data['password'] = md5(I('post.password').$salt);
    			$data['salt'] = $salt;
    		}
    		$data['email'] = I('post.email');
    		session('kfLine'.I('post.rid'),I('post.lineStatus') == '1' ? '1' : null);
    		$keArr = session('kfLineArr');
    		if(I('post.lineStatus') == '1'){
    			if(isset($keArr[I('post.rid')]) && $keArr[I('post.rid')] = I('post.rid')){
    				// Nothing to do
    			}else{
    				$keArr[I('post.rid')] = I('post.rid');
    			}
    			session('kfLineArr',$keArr);
    		}else{
    			if(isset($keArr[I('post.rid')])){
    				unset($keArr[I('post.rid')]);
    				session('kfLineArr',$keArr);
    			}
    		}
    		M('ResEntity')->where(array('rid'=>session('loginUid')))->setField($data);
            putlog('编辑个人资料','UserController','userUpdate');
    		$this->showtips('信息编辑成功');
    	}
    }



    /*
     * 后台管理员管理
     * @date: 2016-8-2 上午10:14:13
     * @editor: liuqi
     */
    public function admin(){
        $page = I('get.page',1);
        $perpage = I('get.perpage',10);
        if(session('role_id') == 2){
           $admin = []; 
        }else{
            $admin = M('resEntity e')->field('e.rid,e.username,r.name,e.is_active,e.email,e.add_at')->
            join('qxww_auth_group_access a on a.uid=e.rid')->
            join('qxww_role r on r.id=a.role_id')->page($page,$perpage)->select();
        }
        if($admin) {
            foreach ($admin as $key => $value) {
                if ($value['name'] == '护士') {
                    $admin[$key]['nurse_name'] = M('NurseEntity')->where(['mobile' => $value['username']])->getField('uname');
                }
            }
        }
        $this->assign('page',setPage(M('resEntity e')->join('qxww_auth_group_access a on a.uid=e.rid')->join('qxww_role r on r.id=a.role_id')->count(),$perpage,$page,'?'.http_build_query($url)));
        $this->assign('getData',$admin);
        $this->display('user/admin');
    }

    /*
     * 冻结&解封用户
     */
    public function adminDecate(){
        if(I('get.types')){
            M('ResEntity')->where(array('rid'=>I('get.resid')))->setField(array('is_active' => '1'));
            $this->showtips('成功解封该用户');
        }
        M('ResEntity')->where(array('rid'=>I('get.resid')))->setField(array(
            'is_active' => '0'
        ));
        $this->showtips('成功冻结该用户');
    }

    /*
     * 后台管理员管理
     * @date: 2016-8-2 上午10:40:23
     * @editor: liuqi
     */
    public function addAdmin(){
    	if(!is_login()){
    		redirect('/user/login');exit;
    	}
        /*用户注册逻辑*/
        if(I('post.submit')){
            if(I('post.rid')){
                $old_data = M('ResEntity')->where(['rid'=>I('post.rid')])->find();
                $data = array('email'=>I('post.email'));
                $update['update_at'] = time();
                if(I('post.password')){
                    $salt = randstr(5);
                    $data['password'] = md5(I('post.password').$salt);
                    $data['salt'] = $salt;
                }
                $data['username'] = I('post.username');
                if(I('post.role_id')){
                    M('AuthGroupAccess')->where(array('uid'=>I('post.rid')))->setField(array('role_id'=>I('post.role_id')));
                    // $data['role_id'] = I('post.role_id');
                }
                if($old_data['username'] != I('post.username')){
                    $update['mobile'] = I('post.username');
                }
                if(I('post.uname')){
                    $update['uname'] = I('post.uname');
                }
                M('ResEntity')->where(array('rid'=>I('post.rid')))->setField($data);
                M('NurseEntity')->where(['mobile'=>$old_data['username']])->setField($update);
                putlog('修改管理员'.$data['username'].'的信息','UserController','addAdmin');
                $this->showtips('信息编辑成功');

            }
            $salts = randstr(5);
            $data = array(
                    'username' => I('post.username'),
                    'password' => md5(I('post.password').$salts),
                    'salt' => $salts,
                    'email' => I('post.email'),
                    'role_id' => I('post.role_id'),
                    'remember_token' => '',
                    'add_at' => time(),
                    'update_at' => time()
                );
            $res = M('ResEntity')->add($data);
            if(I('post.role_id')){
                M('AuthGroupAccess')->add(array('uid'=>$res,'role_id'=>I('post.role_id')));
            }
            putlog('添加管理员'.I('post.username'),'UserController','addAdmin');
            if($res){
                $this->showtips('添加管理员成功');
            }else{
                $this->showtips('操作失败');
            }
        }
        $admin_info = M('resEntity e')->
        join('qxww_auth_group_access a on a.uid=e.rid')->
        join('qxww_role r on r.id=a.role_id')->where(array('e.rid'=>I('get.rid')))->find();
        $admin_info['uname'] = M('NurseEntity')->where(['mobile'=>$admin_info['username']])->getField('uname');
        $this->assign('admin_info',$admin_info);
        $role = M('role')->where(array('status'=>1))->select();
        $this->assign('role',$role);
        //注册页面渲染
        $this->display('user/add_admin');
    }

    /*
     * 添加护士账号
     * @date: 2017-3-22 上午11:40:23
     * @editor: zyy
     */
    public function addNurse(){
        if(!is_login()){
            redirect('/user/login');exit;
        }
        /*用户注册逻辑*/
        if(I('post.dosubmit')){
            if(I('post.mobile')){
                $repeat_m = M('NurseEntity')->where(['mobile'=>I('post.mobile')])->find();
                if($repeat_m){
                    $this->showtips('该手机号已注册');
                }
            }
            if(I('post.card')){
                $repeat_c = M('NurseEntity')->where(['card'=>I('post.card')])->find();
                if($repeat_c){
                    $this->showtips('该身份证号已注册');
                }
            }
            $salt = randstr(5);
            $data = array(
                'uname' => I('post.uname'),
                'mobile' => I('post.mobile'),
                // 'card' => I('post.card'),
                'salt' => $salt,
                'password' => md5(I('post.password').$salt),
                'add_at' => time(),
                'update_at' => time(),
            );
            putlog('添加护士'.I('post.uname'),'UserController','addNurse');
            $res = M('NurseEntity')->add($data);
            if($res){
                $this->showtips('添加护士成功');
            }else{
                $this->showtips('操作失败');
            }
        }
        //注册页面渲染
        $this->display('user/add_nurse');
    }




}
